Imports System.Data.SqlClient
Imports System.ComponentModel
Imports DevComponents.DotNetBar.SuperGrid
Public Class frmCatPuestos
    Private mstrDepartamento As String
    Private mstrValor As String
    Private mstrCondicion As String
    Public WriteOnly Property Condicion() As String

        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property
    Public WriteOnly Property Departamento() As String

        Set(ByVal value As String)
            mstrDepartamento = value
        End Set
    End Property
    Public ReadOnly Property Valor() As String
        Get
            Valor = mstrValor
        End Get
    End Property

    Private Sub frmCatPuestos_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
        End If
    End Sub
    Private Sub frmCatPuestos_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        panelBusqueda.Style.BackColor1.Color = System.Drawing.Color.FromArgb(34, 35, 38)
        panelBusqueda.Style.BackColor2.Color = System.Drawing.Color.FromArgb(34, 35, 38)
        panelBusqueda.Style.BorderColor.Color = System.Drawing.Color.FromArgb(50, 52, 56)

        BotonesAgregar2.BackgroundStyle.BackColor = System.Drawing.Color.FromArgb(28, 28, 31)
        BotonesAgregar2.BackgroundStyle.BackColor2 = System.Drawing.Color.FromArgb(28, 28, 31)
        BotonesAgregar2.BackgroundStyle.BorderColor = System.Drawing.Color.FromArgb(28, 28, 31)
        BotonesAgregar2.BackgroundStyle.BorderColor2 = System.Drawing.Color.FromArgb(28, 28, 31)

        txtBuscarDetalle.BackColor = System.Drawing.Color.FromArgb(18, 19, 20)
        txtBuscarDetalle.ForeColor = System.Drawing.Color.FromArgb(18, 19, 20)

        chTodos.Checked = False

        txtBuscarDetalle.BackColor = System.Drawing.Color.FromArgb(18, 19, 20)
        txtBuscarDetalle.ForeColor = System.Drawing.Color.FromArgb(134, 136, 140)
        BotonesAgregar2.BackgroundStyle.BackColor = System.Drawing.Color.FromArgb(18, 19, 20)
        BotonesAgregar2.BackgroundStyle.BackColor2 = System.Drawing.Color.FromArgb(18, 19, 20)
        BotonesAgregar2.BackgroundStyle.BorderColor = System.Drawing.Color.FromArgb(50, 52, 56)
        BotonesAgregar2.BackgroundStyle.BorderColor2 = System.Drawing.Color.FromArgb(50, 52, 56)

        chTodos.BackColor = Color.WhiteSmoke
        chTodos.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)

        Me.BackColor = Color.WhiteSmoke

        txtBuscarDetalle.Clear()
        txtBuscarDetalle.Focus()


        Try
            CARGA_PUESTOS()
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

       
    End Sub
    Sub CARGA_PUESTOS()


        Dim panel As GridPanel = dgClasificaciones.PrimaryGrid
        dgClasificaciones.BeginUpdate()
        panel.Rows.Clear()


        Dim strSql As String = ""
        Dim strNombre As String = ""

        strNombre = UCase(txtBuscarDetalle.Text.Trim)
        strNombre = "%" & strNombre.Replace(" ", "%") & "%"

        Dim CLAVE As String = BuscarStrDatoCitra("SELECT CLAVE FROM DEPARTAMENTOS WHERE NOMBRE='" & mstrDepartamento & "'")
        strSql = "SELECT P.NOMBRE FROM EMPLEADOS E INNER JOIN PUESTOS P ON E.PUESTO=P.CLAVE WHERE DEPTO='" & CLAVE & "' AND ESTATUS<>'B' AND UPPER(P.NOMBRE) LIKE '" & strNombre & "' " & mstrCondicion & " GROUP BY P.NOMBRE"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Dim CONT As Integer = 0

        Do While rdBuscar.Read()
            Dim ob1() As Object = {rdBuscar("NOMBRE").ToString}
            panel.Rows.Add(New GridRow(ob1))
            CONT = CONT + 1
        Loop
        panel.Footer.Text = CONT & "puestos coinciden con la busqueda"
        dgClasificaciones.EndUpdate()


        If CONT = 0 Then
            btnAceptar.Enabled = False
        Else
            btnAceptar.Enabled = True
        End If

    End Sub
    Sub CARGA_PUESTOS_TODOS()


        Dim panel As GridPanel = dgClasificaciones.PrimaryGrid
        dgClasificaciones.BeginUpdate()
        panel.Rows.Clear()


        Dim strSql As String = ""
        Dim strNombre As String = ""

        strNombre = UCase(txtBuscarDetalle.Text.Trim)
        strNombre = "%" & strNombre.Replace(" ", "%") & "%"

        strSql = "SELECT P.NOMBRE FROM EMPLEADOS E INNER JOIN PUESTOS P ON E.PUESTO=P.CLAVE WHERE ESTATUS<>'B' AND UPPER(P.NOMBRE) LIKE '" & strNombre & "' " & mstrCondicion & " GROUP BY P.NOMBRE"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Dim CONT As Integer = 0

        Do While rdBuscar.Read()
            Dim ob1() As Object = {rdBuscar("NOMBRE").ToString}
            panel.Rows.Add(New GridRow(ob1))
            CONT = CONT + 1
        Loop
        panel.Footer.Text = CONT & "puestos coinciden con la busqueda"
        dgClasificaciones.EndUpdate()


        If CONT = 0 Then
            btnAceptar.Enabled = False
        Else
            btnAceptar.Enabled = True
        End If

    End Sub
    Private Sub dgClasificaciones_Click(sender As System.Object, e As System.EventArgs) Handles dgClasificaciones.Click

    End Sub

    Private Sub dgClasificaciones_DoubleClick(sender As Object, e As System.EventArgs) Handles dgClasificaciones.DoubleClick

    End Sub

    Private Sub dgClasificaciones_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dgClasificaciones.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
        ElseIf e.KeyCode = Keys.Enter Then
            btnAceptar_Click(Me, System.EventArgs.Empty)
        End If
    End Sub

   
    Private Sub chTodos_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If chTodos.Checked = True Then
            CARGA_PUESTOS_TODOS()
        Else
            CARGA_PUESTOS()

        End If
    End Sub

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        If dgClasificaciones.PrimaryGrid.Rows.Count = 1 Then
            Dim ROW As GridRow = dgClasificaciones.PrimaryGrid.GetRowFromIndex(0)
            Dim CODE As String = CStr(ROW.Cells(0).Value)
            mstrValor = CODE
            Me.DialogResult = Windows.Forms.DialogResult.OK
        Else
            For Each articulo In dgClasificaciones.GetSelectedCells
                Dim celda As GridCell = articulo
                Dim ROW As GridRow = dgClasificaciones.PrimaryGrid.GetRowFromIndex(celda.RowIndex)
                Dim CODE As String = CStr(ROW.Cells(0).Value)
                mstrValor = CODE
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Exit For
            Next
        End If
    End Sub

    Private Sub dgClasificaciones_RowDoubleClick(ByVal sender As Object, ByVal e As DevComponents.DotNetBar.SuperGrid.GridRowDoubleClickEventArgs) Handles dgClasificaciones.RowDoubleClick
        btnAceptar_Click(Me, System.EventArgs.Empty)
    End Sub

    Private Sub txtBuscarDetalle_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBuscarDetalle.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
        End If
    End Sub

    Private Sub txtBuscarDetalle_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscarDetalle.TextChanged
        If chTodos.Checked = True Then
            CARGA_PUESTOS_TODOS()
        Else
            CARGA_PUESTOS()

        End If
    End Sub

    
    Private Sub chTodos_CheckedChanged_1(sender As System.Object, e As System.EventArgs) Handles chTodos.CheckedChanged
        If chTodos.Checked = True Then
            CARGA_PUESTOS_TODOS()
        Else
            CARGA_PUESTOS()

        End If
    End Sub
End Class